/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grex.fitnessfunctions.ErrorFunctions;

import grex.GP;
import grex.IPredictiveModel;
import grex.Prediction;
import grex.PredictionContainer;
import grex.fitnessfunctions.FitnessFunction;
import grex.fitnessfunctions.IFitnessFunction;
import grex.genes.Gene;
import grex.genes.GeneException;
import grex.genes.TargetPred;

/**
 *
 * @author RIK
 */
public class MeasureMaxProb extends FitnessFunction{

    public MeasureMaxProb() {
        super("MaxP");
    }
    
    @Override
    protected double calcPredictionError(Prediction prediction, double targetValue) {
        return 0;
    }
    
    @Override
    protected double calcFinalError(PredictionContainer pc){
        if(model!=null && model instanceof GP){
            return(getMaxProb(((GP)model).getHead()));            
        }
        return -1;
    }
    
    private double getMaxProb(Gene gene){
        Gene[] children = gene.getChildren();
        if(children==null)
            return getProb(((TargetPred)gene).getProbs());
        else{
            double left = getMaxProb(children[1]);
            double right = getMaxProb(children[2]);
            return Math.max(left, right);
        }
    }
    
    private double getProb(double[] probs){
        double max =0;
        for (int i = 0; i < probs.length; i++) {
            max = Math.max(max, probs[i]);            
        }
        return max;
    }
    
    public String getName(){
        return "MaxP";
    }


    
}
